<template>
  <el-form :model="form" label-width="120px">
    <el-form-item label="用户名">
      <el-input v-model="form.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="form.password" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save" style="width: 100%">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const form = reactive({
  id: '',
  username: '',
  password: ''
})

onMounted(async() => {
  const str = router.currentRoute.value.query.data
  if(str){
    const data = JSON.parse(str)
    Object.assign(form,data)
  }
})

async function save() {
    let res;
    if(form.id){
      res = await window.db.update(`update user set username=?,password=? where id=?`,form.username,form.password,form.id)
    }else{
      res = await window.db.update('insert into user(username,password) values(?,?)',form.username,form.password)
    }
    
    if(res.changes>0){
        ElMessage.success('保存成功！')
    }else{
        ElMessage.error('保存失败！')
    }
}
</script>

<style lang="scss" scoped></style>
